package com.eee.xiang.disruptor;

import com.eee.xiang.disruptor.consumer.TradeConsumer;
import com.eee.xiang.disruptor.event.Trade;
import com.eee.xiang.disruptor.event.TradeEventFactory;
import com.eee.xiang.disruptor.produce.TradePublisher;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.EventHandlerGroup;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author eee_xiang
 * @Date 2020/5/22
 * @description
 */
public class DisruptorManager{

//    private Disruptor<T> disruptor;

//    public DisruptorManager(Disruptor<T> disruptor){
//        this.disruptor = disruptor;
//    }

    public static void main(String[] args) {

        int ringbuffSize = 1024 * 1024;

        Disruptor<Trade> disruptor = new Disruptor<Trade>(new TradeEventFactory(), ringbuffSize, new MythreadFactory(), ProducerType.SINGLE, new YieldingWaitStrategy());

        EventHandlerGroup<Trade> tradeEventHandlerGroup1 = disruptor.handleEventsWith(new TradeConsumer(), new TradeConsumer());
//        EventHandlerGroup<Trade> tradeEventHandlerGroup = disruptor.handleEventsWithWorkerPool(new TradeConsumer());

        disruptor.start();

        new TradePublisher(disruptor).publishData();

//        disruptor.shutdown();
    }

    static class MythreadFactory implements ThreadFactory{

//        private final ThreadFactory defaultFactory = Executors.defaultThreadFactory();
        private final AtomicInteger threadNumber = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(Thread.currentThread().getThreadGroup(), r,
                    "Disruptor-" + "eee_xiang-"  + threadNumber.getAndIncrement(), 0);
            thread.setDaemon(false);
            return thread;
        }
    }

}
